﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Collections;
using DevExpress.XtraTreeList.Nodes;
using System.Threading;
namespace TS_XY_Train_Repaire.UserCon.Report
{
    public partial class uc_Report_WorkSteps_Relation_ManaForm : DevExpress.XtraEditors.XtraForm
    {
        public string _LoadID = "";
        public string _LoadName = "";
        public uc_Report_WorkSteps_Relation_ManaForm()
        {
            InitializeComponent();
        }

        private void uc_Report_WorkSteps_Relation_ManaForm_Load(object sender, EventArgs e)
        {
            this.Text = _LoadName+"->>关联作业工步管理";
            _LoadOrgList();
            _LoadAuthGrid();
        }
        #region//--作业工步--//
        private void _LoadOrgList()
        {
            TR_PS_Work_Main_List _tl = new TR_PS_Work_Main_List();
            tl_Main.Nodes.Clear();
            ArrayList pList = new ArrayList();
            _tl = new TR_PS_Work_Main_List();
            _tl.f_id = "W0";
            _tl.f_name = "整车作业流程目录";
            _tl.f_imgindex = 14;
            pList.Add(_tl);
            _tl = new TR_PS_Work_Main_List();
            _tl.f_id = "W1";
            _tl.f_name = "转向架作业流程目录";
            _tl.f_imgindex = 14;
            pList.Add(_tl);
            _tl = new TR_PS_Work_Main_List();
            _tl.f_id = "W2";
            _tl.f_name = "电器作业流程目录";
            _tl.f_imgindex = 14;
            pList.Add(_tl);
            string strSQL = "select * from TS_TR_PS_Work_Main where f_del=0";
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            SqlCommand cmd = new SqlCommand(strSQL, conn);
            SqlDataReader read = cmd.ExecuteReader();
            while (read.Read())
            {
                _tl = new TR_PS_Work_Main_List();
                _tl.f_id = "M" + read["f_id"].ToString();
                _tl.f_name = read["f_name"].ToString();
                _tl.f_parent = "W" + read["f_type"].ToString();
                _tl.f_rpid = read["f_rpid"].ToString();
                _tl.f_ck = false;
                _tl.f_imgindex = 10;
                pList.Add(_tl);
            }
            read.Close();
            strSQL = "select * from TS_TR_PS_Work_Main_Day where f_mwid in (select f_id from TS_TR_PS_Work_Main where f_del=0) and f_del=0 order by f_mwid,f_order asc";
            cmd = new SqlCommand(strSQL, conn);
            read = cmd.ExecuteReader();
            while (read.Read())
            {
                _tl = new TR_PS_Work_Main_List();
                _tl.f_id = "D" + read["f_id"].ToString();
                _tl.f_ck = false;
                _tl.f_order = read["f_order"].ToString();
                _tl.f_name = read["f_name"].ToString();
                _tl.f_mwid = read["f_mwid"].ToString();
                _tl.f_mwdid = read["f_id"].ToString();
                _tl.f_length = read["f_length"].ToString() + "天";
                _tl.f_parent = "M" + read["f_mwid"].ToString();
                _tl.f_imgindex = 11;
                pList.Add(_tl);
            }
            read.Close();
            strSQL = "select *,(select count(*) from TS_S_T_P_Report_Tech where f_del=0 and f_treeid="+_LoadID.Substring(1)+ " and f_functype=1 and f_funcid=TS_TR_PS_Work_Main_Steps.f_id) as f_count  from TS_TR_PS_Work_Main_Steps where f_mwdid in (select f_id from TS_TR_PS_Work_Main_Day where f_mwid in (select f_id from TS_TR_PS_Work_Main where f_del=0) and f_del=0) and f_del=0 order by f_mwid,f_mwdid,f_order asc";
            cmd = new SqlCommand(strSQL, conn);
            read = cmd.ExecuteReader();
            while (read.Read())
            {
                _tl = new TR_PS_Work_Main_List();
                _tl.f_id = "S" + read["f_id"].ToString();
                _tl.f_order = read["f_order"].ToString();
                _tl.f_name = read["f_name"].ToString();
                _tl.f_mwid = read["f_mwid"].ToString();
                _tl.f_mwdid = read["f_mwdid"].ToString();
                _tl.f_mwsid = read["f_id"].ToString();
                _tl.f_length = read["f_length"].ToString() + "小时"; ;
                _tl.f_stime = read["f_stime"].ToString();
                _tl.f_etime = read["f_etime"].ToString();
                _tl.f_parent = "D" + read["f_mwdid"].ToString();
                _tl.f_imgindex = 12;
                if (read["f_count"].ToString() == "0")
                {
                    _tl.f_ck = false;
                }
                else
                {
                    _tl.f_ck = true;
                }
                pList.Add(_tl);
            }
            read.Close();
            conn.Close();
            conn.Dispose();
            tl_Main.DataSource = pList;
            tl_Main.ExpandAll();
        }
        #endregion
        private void _LoadAuthGrid()
        {
            string strSQL = "select f_id,convert(bit,0) as f_ck,row_number() over (order by f_id) as f_row,(select f_name from TS_TR_PS_Work_Main_Steps where f_id=TS_S_T_P_Report_Tech.f_funcid) as f_name  from TS_S_T_P_Report_Tech where f_treeid=" + _LoadID.Substring(1) + " and f_del=0";
            DataTable dt = new DataTable();
            TS_Win_Class.FilledDataTable(strSQL, dt);
            gc_Main.DataSource = dt;
        }
        private void bt_Exit_Click(object sender, EventArgs e)
        {
            this.DialogResult=DialogResult.OK;
        }

        private void bt_Sure_Click(object sender, EventArgs e)
        {
            string strSQL = "";
            string _FuncList = GetFunclIst(tl_Main.Nodes, "");
            TS_Win_Class.MySqlCom("update TS_S_T_P_Report_Tech set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId+",f_time=getdate() where f_treeid=" + _LoadID.Substring(1)+" and f_functype=1 and f_del=0");
            if (_FuncList.Length > 0)
            {
                string[] _arry = _FuncList.Split(',');
                for (int i = 0; i < _arry.Length; i++)
                {
                    if (_arry[i].Length > 0)
                    {
                        strSQL += "insert into TS_S_T_P_Report_Tech(f_treeid,f_funcid,f_functype,f_workerid) values(" + _LoadID.Substring(1) + "," + _arry[i].Substring(1) + ",1," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ");";
                    }
                }
                TS_Win_Class.MySqlCom(strSQL);
            }
            Thread.Sleep(50);
            _LoadAuthGrid();
        }

        private void bt_Del_Click(object sender, EventArgs e)
        {
            string _str = "";
            for (int i = 0; i < gv_Main.DataRowCount; i++)
            {
                if ((bool)gv_Main.GetRowCellValue(i, "f_ck"))
                {
                    _str += gv_Main.GetRowCellValue(i, "f_id").ToString()+",";
                }
            }
            _str = _str.Substring(0, _str.Length-1);
            TS_Win_Class.MySqlCom("update TS_S_T_P_Report_Tech set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where  f_id in(" + _str + ")");
            _LoadOrgList();
            _LoadAuthGrid();
        }
        private string GetFunclIst(TreeListNodes _Nodes, string _str)
        {
            string _Str = _str;
            foreach (TreeListNode node in _Nodes)
            {
                if ((bool)node.GetValue("f_ck") && node.GetValue("f_id").ToString().Substring(0,1)=="S")
                {
                    _Str += node.GetValue("f_id").ToString() + ",";
                }
                if (node.HasChildren)
                {
                    _Str = GetFunclIst(node.Nodes, _Str);
                }
            }
            return _Str;
        }

        private void uc_Report_WorkSteps_Relation_ManaForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }
    }
}